package com.smartx.app.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.smartx.app.R;
import com.smartx.config.Constants;

public class MnemonicDialog extends DialogFragment implements View.OnClickListener{

    public final static String MNEMONIC = "MNEMONIC";
    private TextView mnemonicTextView = null;
    private Button copyButton = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置背景透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view= LayoutInflater.from(getActivity()).inflate(R.layout.dialog_mnmonic, null);
        copyButton= view.findViewById(R.id.copy_action);
        mnemonicTextView = view.findViewById(R.id.tx_mnemonic);
        copyButton.setOnClickListener(this);

        Bundle bundle = getArguments();
        String mnemonic = bundle.getString(MNEMONIC);
        mnemonicTextView.setText(mnemonic);

        builder.setView(view);
        return builder.create();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.copy_action:
                if (getTargetFragment()== null){
                    return;
                }
                ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData mClipData = ClipData.newPlainText("", mnemonicTextView.getText());
                cm.setPrimaryClip(mClipData);

                Intent intent= new Intent();
                intent.putExtra(MNEMONIC, mnemonicTextView.getText().toString());
                getTargetFragment().onActivityResult(Constants.REQUEST_CODE_COPY_MNEMONIC, Activity.RESULT_OK, intent);
                break;
        }
    }
}
